﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ZhaoXi.Control.Components
{
    public class ComponentBase:UserControl
    {
        //是否选中
        private bool _IsSelected;

        public bool IsSelected
        {
            get { return _IsSelected; }
            set
            {
                _IsSelected = value;
                //当设置IsSelected属性的值时，它会更新_IsSelected的值，并根据新值执行不同的逻辑。
                //如果新值为true，它会遍历当前组件的父级Grid，将所有ComponentBase类型的子组件的IsSelected属性设置为false。
                //然后，它会使用VisualStateManager.GoToState方法根据当前的选中状态切换组件的视觉状态，分别切换到"SelectedState"或"UnSelectedState"。
                //当获取IsSelected属性的值时，它简单地返回_IsSelected的值。
                if (value)
                {
                    var parent = VisualTreeHelper.GetParent(this) as Grid;
                    if (parent != null) {
                        foreach (var item in parent.Children)
                        {
                            if (item is ComponentBase)
                            {
                                (item as ComponentBase).IsSelected = false;
                            }
                        }
                    }
                }

                VisualStateManager.GoToState(this, value ? "SelectedState" : "UnSelectedState", false);
            }
        }

        //是否进行中
        public bool IsRunning
        {
            get { return (bool)GetValue(IsRunningProperty); }
            set { SetValue(IsRunningProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsRunning.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsRunningProperty =
            DependencyProperty.Register("IsRunning", typeof(bool), typeof(ComponentBase), new PropertyMetadata(default(bool), new PropertyChangedCallback(OnRunningStateChanged)));



        public static void OnRunningStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            VisualStateManager.GoToState(d as ComponentBase, (bool)e.NewValue ? "RunState" : "StopState", false);
        }


        //是否报错
        public bool IsFault
        {
            get { return (bool)GetValue(IsFaultProperty); }
            set { SetValue(IsFaultProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsFault.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsFaultProperty =
            DependencyProperty.Register("IsFault", typeof(bool), typeof(ComponentBase), new PropertyMetadata(default(bool), new PropertyChangedCallback(OnFaultStateChanged)));


        public static void OnFaultStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            VisualStateManager.GoToState(d as ComponentBase, (bool)e.NewValue ? "NormalState" : "FaultState", false);
        }



        //行为
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(ComponentBase), new PropertyMetadata(default(ICommand)));



        //行为参数
        public Object CommandParameter
        {
            get { return (Object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(Object), typeof(ComponentBase), new PropertyMetadata(default(Object)));

        public ComponentBase()
        {
            //添加左键按钮按下事件
            this.PreviewMouseLeftButtonDown += CoolingTower_PreviewMouseLeftButtonDown;
        }

        private void CoolingTower_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.IsSelected = !this.IsSelected;
            this.Command?.Execute(this.CommandParameter);
            e.Handled = true;
        }
    }
}
